package okhttp3Demo.szse.data.demo;

import com.alibaba.fastjson.JSON;
import jxl.Cell;
import jxl.Sheet;
import jxl.Workbook;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;
import okhttp3Demo.OkHttpClientFactory;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
import utils.TxtUtils;

import java.io.File;
import java.io.IOException;
import java.net.URLEncoder;
import java.time.LocalTime;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;

public class Demo {

    public static String baseDir = "C:/Users/Administrator/Desktop/深圳证交所/";

    public static void main(String[] args) throws Exception {
        int maxPage = 417;
        ExecutorService executorService = Executors.newFixedThreadPool(40);
        Demo demo = new Demo();
        List<String> pageList =  demo.getPageList(maxPage);
        pageList.forEach( page-> {
            executorService.execute(()->{
                new Demo().genData(page);
            });
        });
        executorService.shutdown();
        while (!executorService.awaitTermination(1, TimeUnit.SECONDS)){
            System.out.println("心跳");
        }
    }


    public void genData(String page){
        try {
            String url = new StringBuilder("http://www.szse.cn/api/report/ShowReport/data?SHOWTYPE=JSON&CATALOGID=1760_zsn&TABKEY=tab1&PAGENO=")
                    .append(page)
                    .append("&random=0.30476371042374883")
                    .toString();
            OkHttpClient client = OkHttpClientFactory.getInstance().getClient(OkHttpClientFactory.SZZJ_DATA);
            Request request = new Request.Builder().url(url).get()
                    .addHeader("Content-Type","application/json;charset=UTF-8")
                    .build();
            Response response = client.newCall(request).execute();
            String resultStr = response.body().string();
            TxtUtils.append(new File(baseDir + page +"data.txt"),resultStr+",");
        } catch (Exception e) {
            e.printStackTrace();
        }
    }


    public List<String> getPageList(int maxPage){
        List<String> list = new ArrayList<>();
        for(int i = 1;i<=maxPage;i++){
            list.add(String.valueOf(i));
        }
        return list;
    }




}
